home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / userbox / publicdomain / edspell / utils / delword.c < prev    next >
C/C++ Source or Header  |  1996-07-27  |  4KB  |  166 lines

  1. /************************************************************************/
  2. /*
  3. /*  Filename : DelWord.c
  4. /*
  5. /*   Version : 1.0
  6. /*      Date : 28 Jul 1996
  7. /*    Author : Martin Reddy <M.Reddy@ed.ac.uk>
  8. /*
  9. /*   Purpose : Removes all entries from a file which contain a line with
  10. /*             a specified text on it. This is used in order to remove
  11. /*             entries from the user dictionary used by the ISpell spell
  12. /*             checking package.
  13. /*
  14. /*     Usage : DelWord <word> [<UserDictFilename>]
  15. /*   Returns : 0  = word deleted successfully from the dictionary
  16. /*             1  = word did not exist in the dictionary
  17. /*             >1 = a (disk) error occurred
  18. /*
  19. /************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25.  
  26. /* define the filename for the default user dictionary */
  27.  
  28. #define DEFAULT_USER_DICT "EdSpell:Dict/UserDict.txt"
  29.  
  30. /* the name of the temporary file to write out to */
  31.  
  32. #define TMP_FILENAME "EdSpell:Dict/UserDict.tmp"
  33.  
  34. /* some default string sizes and the standard boolean stuff */
  35.  
  36. #define MAX_FILENAME_SIZE 100
  37. #define MAX_WORD_SIZE 100
  38.  
  39. #define TRUE 1
  40. #define FALSE 0
  41.  
  42. /* setup the filename and search string variables for this module */
  43.  
  44. char dictFilename[MAX_FILENAME_SIZE] = DEFAULT_USER_DICT;
  45. char searchWord[MAX_WORD_SIZE];
  46.  
  47. char version[] = "$VER: DelWord V1.0 (28/7/96)";
  48.  
  49. /*
  50.  * parseOptions: Checks the command line options for any valid options
  51.  *
  52.  */
  53.  
  54. void parseOptions( int argc, const char *argv[] )
  55. {
  56.     int i;
  57.  
  58.     /* output a little program info if the command line args are wrong */
  59.  
  60.     if ( argc < 2 || argc > 3 ) {
  61.         fprintf( stderr, "usage: DelWord <word> [<dictfile>]\n" );
  62.         fprintf( stderr, "purpose: remove a word from an ISpell user dictionary.\n" );
  63.         fprintf( stderr, "author: Martin Reddy, 28 Jul 1996. Version 1.0\n" );
  64.         exit( EXIT_FAILURE );
  65.     }
  66.  
  67.     /* copy the supplied command line arguments appropriately */
  68.  
  69.     strcpy( searchWord, argv[1] );
  70.     if ( argc == 3 ) strcpy( dictFilename, argv[2] );
  71.  
  72.     /* ISpell entries are all in uppercase, so change search word into */
  73.     /* upper case so that we can do a case sensitive search. Also, we  */
  74.     /* append a CR character for compatibility with stdio's fgets()    */
  75.  
  76.     for ( i = 0; i < strlen( searchWord ); ++i )
  77.         searchWord[i] = toupper( searchWord[i] );
  78.     strcat( searchWord, "\n" );
  79. }
  80.  
  81. /*
  82.  * openFiles: open the input dictionary file and the temporary output file
  83.  *
  84.  */
  85.  
  86. void openFiles( FILE **infile, FILE **outfile )
  87. {
  88.     /* open the dictionary file for reading */
  89.  
  90.     if ( ( *infile = fopen( dictFilename, "r" ) ) == NULL ) {
  91.         fprintf( stderr, "Cannot find user dictionary: %s\n", dictFilename );
  92.         exit( EXIT_FAILURE );
  93.     }
  94.  
  95.     /* open a temporary file to write the new dictionary to */
  96.  
  97.     if ( ( *outfile = fopen( TMP_FILENAME, "w" ) ) == NULL ) {
  98.         fclose( *infile );
  99.         fprintf( stderr, "Cannot create temp file: %s\n", TMP_FILENAME );
  100.         exit( EXIT_FAILURE );
  101.     }
  102. }
  103.  
  104. /*
  105.  * main: the main program body - where it all happens!
  106.  *
  107.  */
  108.  
  109. int main( int argc, char *argv[] )
  110. {
  111.     FILE *infile, *outfile;
  112.     char wordBuffer[MAX_WORD_SIZE];
  113.     int  deletedWords = 0;
  114.  
  115.     /* parse the command line to get the word to remove from dictionary */
  116.  
  117.     parseOptions( argc, argv );
  118.  
  119.     /* prepare the dictionary and temporary files for reading/writing */
  120.  
  121.     openFiles( &infile, &outfile );
  122.  
  123.     /* go through each word in the dictionary... */
  124.  
  125.     fgets( wordBuffer, MAX_WORD_SIZE, infile );
  126.     while ( feof( infile ) == FALSE ) {
  127.  
  128.         /* if the word is not the one we want to remove, then output it */
  129.         /* to the temporary file that we are building.                  */
  130.  
  131.         if ( strcmp( wordBuffer, searchWord ) != 0 ) {
  132.             if ( fprintf( outfile, wordBuffer ) != strlen( wordBuffer ) ) {
  133.                 fprintf( stderr, "Error writing to %s (disk full?)\n",
  134.                          TMP_FILENAME );
  135.                 fclose( infile );
  136.                 fclose( outfile );
  137.                 remove( TMP_FILENAME );
  138.                 exit( EXIT_FAILURE );
  139.             }
  140.         } else
  141.             ++deletedWords;
  142.  
  143.         /* then get the next word from the file... */
  144.  
  145.         fgets( wordBuffer, MAX_WORD_SIZE, infile );
  146.     }
  147.  
  148.     /* close all open files, overwrite the original dictionary file */
  149.     /* with the temporary one (if changed), and then exit           */
  150.  
  151.     fclose( infile );
  152.     fclose( outfile );
  153.  
  154.     if ( deletedWords > 0 ) {
  155.         remove( dictFilename );
  156.         rename( TMP_FILENAME, dictFilename );
  157.         return EXIT_SUCCESS;
  158.     } else {
  159.         remove( TMP_FILENAME );
  160.         return 1;
  161.     }
  162. }
  163.  
  164. /* EOF: DelWord.c */
  165.  
  166.